// timer.c -- Initialises the PIT, and handles clock updates.
//            Written for JamesM's kernel development tutorials.

#include <timer.h>
#include <isr.h>
#include <display.h>


static void keyboard_callback(registers_t regs)
{
    UNUSED_PARAMETER(regs);
    uint8_t scan_code = inb(0x60);

    if((scan_code & 128) == 128)
    {
        printf("\nkey_released code : %d\n", scan_code);
    }
    else
    {
        printf("\nkey_pressed code : %d\n", scan_code);
    }
    outb(0x20, 0x20);
}
void init_timer(uint32_t frequency)
{
    register_interrupt_handler(IRQ1, &keyboard_callback);

    // The value we send to the PIT is the value to divide it's input clock
    // (1193180 Hz) by, to get our required frequency. Important to note is
    // that the divisor must be small enough to fit into 16-bits.
    uint32_t divisor = 1193180 / frequency;

    // Send the command byte.
    outb(0x43, 0x36);

    // Divisor has to be sent byte-wise, so split here into upper/lower bytes.
    uint8_t l = (uint8_t)(divisor & 0xFF);
    uint8_t h = (uint8_t)( (divisor>>8) & 0xFF );

    // Send the frequency divisor.
    outb(0x40, l);
    outb(0x40, h);
}
